package com.example.lanouhn.bantangapp.activities.fragments;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.example.lanouhn.bantangapp.R;
import com.example.lanouhn.bantangapp.activities.adapters.Adapter_Commsun_hotter;
import com.example.lanouhn.bantangapp.activities.contants.DT_sun;
import com.example.lanouhn.bantangapp.activities.contants.DT_sun_Image;
import com.example.lanouhn.bantangapp.activities.contants.DT_sun_two;
import com.example.lanouhn.bantangapp.activities.contants.FullyGridLayoutManager;
import com.example.lanouhn.bantangapp.activities.contants.FullyLinearLayoutManager;
import com.example.lanouhn.bantangapp.activities.contants.MyRecyclerView1;
import com.example.lanouhn.bantangapp.activities.contants.MyScrollview;
import com.example.lanouhn.bantangapp.activities.contants.SlideShowView;
import com.example.lanouhn.bantangapp.activities.utils.HttpUtils;
import com.example.lanouhn.bantangapp.activities.utils.JsonUtils_DT_sun;
import com.example.lanouhn.bantangapp.activities.utils.JsonUtils_DT_sun_two;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lanouhn on 16/7/23.
 * 晒单页面
 */
public class Fragment_Commsun extends Fragment implements XRecyclerView.LoadingListener {


    private XRecyclerView rehotter;//分类的recyclerview

    private List<DT_sun> dataList;//文本
    private List<DT_sun_two> sunlist = new ArrayList<>();
    ;//热门实体类
    private Adapter_Commsun_hotter adapter_commsun_hotter;//数据适配器

    private List<LinearLayout> viewList;
    private SlideShowView slideshowView;

    private int i = 0;//页数


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.community_thesun_fragment, container, false);

        rehotter = (XRecyclerView) view.findViewById(R.id.rehotter);

        rehotter.setPullRefreshEnabled(false);
        rehotter.setLoadingMoreEnabled(true);
        rehotter.setLoadingListener(this);

        GridLayoutManager manager = new GridLayoutManager(getActivity(),3);
        manager.setOrientation(GridLayoutManager.VERTICAL);

        rehotter.setLayoutManager(manager);

        View headView = inflater.inflate(R.layout.community_thesun_fragment_head, container, false);
       slideshowView = (SlideShowView) headView.findViewById(R.id.slideshowView);

        rehotter.addHeaderView(headView);

        return view;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
        initDataHotter();//热门数据
    }

    private void initDataHotter() {
        //晒单热门数据


        new Thread(new Runnable() {
            @Override
            public void run() {
                String url = DT_sun_Image.hotterURL;
                String parmas = "app_id=com.jzyd.BanTang&client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&track_user_id=2425199&oauth_token=f1ba94f80125dbe645e30f80634437d0&track_deviceid=868596020655823&track_device_info=m2+note&channel_name=UMENG_CHANNEL&app_installtime=1468811189&app_versions=5.8.4&os_versions=5.1&screensize=1080&v=16&page=" + i + "&pagesize=18";

                String result = HttpUtils.doPost(url, parmas);

                if (sunlist.size() > 0)
                    sunlist.addAll(JsonUtils_DT_sun_two.getdtsunList(result));
                else
                    sunlist = JsonUtils_DT_sun_two.getdtsunList(result);

                if (sunlist.size() > 0) {


                    handler.sendEmptyMessage(2);

                } else {
                    handler.sendEmptyMessage(0);
                }
            }
        }).start();
    }

    //绑定数据
    private void initData() {

        dataList = new ArrayList<>();

        viewList = new ArrayList<>();


        new Thread(new Runnable() {
            @Override
            public void run() {
                String result = HttpUtils.doGet(DT_sun_Image.DT_LIST_URL);

                dataList = JsonUtils_DT_sun.getdtsunList(result);
                if (dataList.size() > 0) {

                    Message msg = new Message();
                    handler.sendEmptyMessage(1);

                } else {
                    handler.sendEmptyMessage(0);
                }
            }
        }).start();

    }

    /**
     * 获取子Item的布局和控件，并绑定数据和事件
     *
     * @param sun
     * @return
     */
    public View getChildItem(final DT_sun sun) {
        View item_item = LayoutInflater.from(getActivity()).inflate(R.layout.item_item, null);
        ImageView iv = (ImageView) item_item.findViewById(R.id.image);

        Picasso.with(getActivity()).load(sun.getPic()).into(iv);

//        item_item.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                Toast.makeText(getActivity(), "你点击了" + sun.getName(), Toast.LENGTH_SHORT).show();
//            }
//        });
        return item_item;
    }

    //判断网络是否连接
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:

                    //单个图片集合
                    List<View> itemChilds = new ArrayList<>();
                    for (int i = 0; i < dataList.size(); i++) {
                        itemChilds.add(getChildItem(dataList.get(i)));
                    }
                    //单行集合
                    List<LinearLayout> itemLL = new ArrayList<>();
                    LinearLayout ll = null;
                    for (int i = 0; i < itemChilds.size(); i++) {

                        if (i % 2 == 0) {
                            ll = new LinearLayout(getActivity());
                            ll.setOrientation(LinearLayout.HORIZONTAL);
                            ll.addView(itemChilds.get(i));
                        } else {
                            ll.addView(itemChilds.get(i));
                            itemLL.add(ll);
                        }

                    }
                    //单页集合
                    LinearLayout itemTotal = null;
                    for (int i = 0; i < itemLL.size(); i++) {

                        if (i % 3 == 0) {

                            if (itemTotal != null)
                                viewList.add(itemTotal);

                            itemTotal = (LinearLayout) LayoutInflater.from(getActivity()).inflate(R.layout.vp_item, null);
                        }

                        itemTotal.addView(itemLL.get(i));

                        if (i == itemLL.size() - 1)
                            viewList.add(itemTotal);

                    }


                    slideshowView.initItemViewPager(viewList);

                    break;
                case 0:
                    Toast.makeText(getActivity(), R.string.fail, Toast.LENGTH_SHORT).show();
                    break;
                case 2:

                    //设置Gridview  三列 线性排布


                    if (sunlist.size() > 0 && adapter_commsun_hotter != null)
                        adapter_commsun_hotter.notifyDataSetChanged();
                    else {
                        adapter_commsun_hotter = new Adapter_Commsun_hotter(getActivity(), sunlist);
                        rehotter.setAdapter(adapter_commsun_hotter);
                    }
                    if (adapter_commsun_hotter == null) {
                        Toast.makeText(getActivity(), R.string.see, Toast.LENGTH_SHORT).show();
                    }
                    rehotter.loadMoreComplete();


                    break;

            }
        }
    };


    @Override
    public void onRefresh() {

    }

    @Override
    public void onLoadMore() {
        i++;
        // 获取网络数据
        initDataHotter();
    }
}
